<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="cn">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.0" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="categories">
<meta property="og:url" content="http://yoursite.com/categories/index.html">
<meta property="og:site_name" content="hill&#39;s blog">
<meta property="og:locale" content="cn">
<meta property="article:published_time" content="2017-07-10T08:36:26.000Z">
<meta property="article:modified_time" content="2021-02-25T04:04:48.836Z">
<meta property="article:author" content="heyaojia">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/categories/"/>





  <title>Nosql/Redis-进阶 | hill's blog</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="cn">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">hill's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/04/21/Nosql/Redis-%E8%BF%9B%E9%98%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="heyaojia">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hill's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Nosql/Redis-进阶</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2021-04-21T11:55:47+08:00">2021-04-21</time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Redis-进阶"><a href="#Redis-进阶" class="headerlink" title="Redis - 进阶"></a>Redis - 进阶</h3><hr>
<h4 id="一、Redis-配置详解"><a href="#一、Redis-配置详解" class="headerlink" title="一、Redis 配置详解"></a>一、<strong><em>Redis</em></strong> 配置详解</h4><p>​        只说明一些常用的，其他在对Redis深入理解的时候，用到再来看注解。</p>
<ol>
<li>常用配置</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## INCLUDES </span></span><br><span class="line"><span class="comment">## 标签下可以配置多份config，远程调用，集群时使用</span></span><br><span class="line">	include /path/to/local.conf</span><br><span class="line">	include /path/to/other.conf</span><br><span class="line"></span><br><span class="line"><span class="comment">###1. NETWORK 网络链接</span></span><br><span class="line">    <span class="comment"># bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 		addresses</span></span><br><span class="line">    <span class="comment"># bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6</span></span><br><span class="line">	<span class="built_in">bind</span> * -::*                     <span class="comment"># 通配符全部ip都可以</span></span><br><span class="line"></span><br><span class="line">	protected-mode yes <span class="comment">##保护模式（默认开启）</span></span><br><span class="line"></span><br><span class="line">	port 6379 <span class="comment">##端口</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>重点配置</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">###1. GENERAL</span></span><br><span class="line">	daemonize no <span class="comment">##是否守护进程模式开启，后台运行</span></span><br><span class="line"></span><br><span class="line">	supervised auto</span><br><span class="line"></span><br><span class="line">	pidfile /var/run/redis_6379.pid <span class="comment">### pid文件</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">## 日志级别</span></span><br><span class="line">    <span class="comment"># debug (a lot of information, useful for development/testing)</span></span><br><span class="line">    <span class="comment"># verbose (many rarely useful info, but not a mess like the debug 		level)</span></span><br><span class="line">    <span class="comment"># notice (moderately verbose, what you want in production probably)</span></span><br><span class="line">    <span class="comment"># warning (only very important / critical messages are logged)</span></span><br><span class="line">	loglevel notice</span><br><span class="line"></span><br><span class="line">	logfile <span class="string">""</span> <span class="comment">##日志地址</span></span><br><span class="line">	</span><br><span class="line">	databases 16 <span class="comment">## 数据库数量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###2. SNAPSHOTTING 快照 - 持久化</span></span><br><span class="line">	<span class="comment">### RDB相关</span></span><br><span class="line">    save 3600 1		<span class="comment">#   * 一小时至少1个key change了</span></span><br><span class="line">    save 300 100	<span class="comment">#   * 五分钟至少100个key change了</span></span><br><span class="line">    save 60 10000	<span class="comment">#   * 六十秒10000个key change了</span></span><br><span class="line">    </span><br><span class="line">	stop-writes-on-bgsave-error yes <span class="comment">#持久化是否继续工作</span></span><br><span class="line"></span><br><span class="line">	rdbcompression yes <span class="comment">#rdb压缩 额外消耗X%的cpu,网上说的是10%</span></span><br><span class="line"></span><br><span class="line">	rdbchecksum yes <span class="comment">#检查rdb</span></span><br><span class="line"></span><br><span class="line">	dbfilename dump.rdb<span class="comment">#rdb 目录</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###3. REPLICATION (特重点，主从复制)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###4. MEMORY MANAGEMENT</span></span><br><span class="line">	maxmemory &lt;bytes&gt; <span class="comment">##最大内存</span></span><br><span class="line">	</span><br><span class="line">	maxmemory-policy noeviction <span class="comment">## 达到最大内存后的策略</span></span><br><span class="line">	</span><br><span class="line">    <span class="comment"># volatile-lru -&gt; Evict using approximated LRU, only keys with an expire set. </span></span><br><span class="line">    <span class="comment">#删除过期key</span></span><br><span class="line">    <span class="comment"># allkeys-lru -&gt; Evict any key using approximated LRU.</span></span><br><span class="line">    <span class="comment"># volatile-lfu -&gt; Evict using approximated LFU, only keys with an expire set.</span></span><br><span class="line">    <span class="comment"># allkeys-lfu -&gt; Evict any key using approximated LFU.</span></span><br><span class="line">    <span class="comment"># volatile-random -&gt; Remove a random key having an expire set.</span></span><br><span class="line">    <span class="comment"># 随机删除过期的key</span></span><br><span class="line">    <span class="comment"># allkeys-random -&gt; Remove a random key, any key. </span></span><br><span class="line">    <span class="comment"># 随机删除</span></span><br><span class="line">    <span class="comment"># volatile-ttl -&gt; Remove the key with the nearest expire time (minor TTL) </span></span><br><span class="line">    <span class="comment"># 删除临近过期的</span></span><br><span class="line">    <span class="comment"># noeviction -&gt; Don't evict anything, just return an error on write operations. </span></span><br><span class="line">    <span class="comment"># 直接写报错</span></span><br><span class="line"></span><br><span class="line">    maxmemory-eviction-tenacity 10</span><br><span class="line">    </span><br><span class="line"><span class="comment">### 5. APPEND ONLY MODE</span></span><br><span class="line"></span><br><span class="line">	appendonly no <span class="comment">## 默认不开启</span></span><br><span class="line">    </span><br><span class="line">    appendfilename <span class="string">"appendonly.aof"</span> <span class="comment">## 文件名称</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">##同步频率</span></span><br><span class="line">	appendfsync everysec（最多丢失1s的操作）\always\no</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>额外配置</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 设置单位，大小写不敏感</span></span><br><span class="line">    1k =&gt; 1000 bytes</span><br><span class="line">    1kb =&gt; 1024 bytes</span><br><span class="line">    1m =&gt; 1000000 bytes</span><br><span class="line">    1mb =&gt; 1024*1024 bytes</span><br><span class="line">    1g =&gt; 1000000000 bytes</span><br><span class="line">    1gb =&gt; 1024*1024*1024 bytes</span><br><span class="line"></span><br><span class="line"><span class="comment">###1. SECURITY</span></span><br><span class="line">	requirepass foobared <span class="comment">#设置密码</span></span><br><span class="line"><span class="comment">###2. CLIENTS</span></span><br><span class="line">	maxclients 10000  <span class="comment">#最大客户端数</span></span><br></pre></td></tr></table></figure>



<h4 id="二、Redis持久化"><a href="#二、Redis持久化" class="headerlink" title="二、Redis持久化"></a>二、<strong><em>Redis</em></strong>持久化</h4><p>​        Redis是内存数据库，但是有些时候数据是需要持久化操作的。Redis提供了RDB和AOF两种方式来持久化。</p>
<ol>
<li><p><strong><em>RDB (Redis Database)</em></strong></p>
<p>（下图是<strong><em>RDB</em></strong>的作用机制）</p>
<p>![xxx](/Users/heyaojia/hill/other/myhexo/source/Redis-基础/截屏2021-04-12 下午10.09.34.png) </p>
<p>​    在指定时间dump到磁盘，Redis会fork一个进程出来持久化，先写到一个临时文件，后面在替换上次持久化的文件。在对数据不敏感的一些业务场景，RDB性能比较好，因为主进程<strong><em>不需要进行任何IO操作</em></strong>。因为如果在最后一次持久化的时候，断电、宕机操作，会丢数据。</p>
</li>
<li><p><strong><em>AOF (Append Of File )</em></strong></p>
<p>​    AOF持久化是以追加操作日志的方式去进行持久化。与RDB相比，AOF的性能消耗会高，但是数据会相较安全。同样，最后一（s）的APPEND操作，假设断电，至少丢失一条数据。</p>
<p>（下图是<strong><em>AOF</em></strong>的作用机制）</p>
</li>
</ol>
<ol start="3">
<li><p>两种方式的<strong><em>触发机制</em></strong></p>
<ol>
<li>RDB相较于AOF的话性能更好，但是数据可靠性偏低</li>
<li>AOF相较于RDB数据完整性高一些，但是性能开销高一点</li>
<li>在实际生产中，AOF、RDB不冲突，可以同时开启</li>
</ol>
</li>
<li><p>数据恢复详情</p>
</li>
</ol>
<h4 id="三、Redis-主从复制"><a href="#三、Redis-主从复制" class="headerlink" title="三、Redis 主从复制"></a>三、<strong><em>Redis</em></strong> <strong>主从复制</strong></h4><p>​        为了打造一个高可用的数据系统，Redis提供了主从复制的机制。</p>
<p><strong><em>| 主从机配置修改</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">pidfile /var/run/redis_6380.pid</span><br><span class="line"></span><br><span class="line">logfile <span class="string">"6380.log"</span></span><br><span class="line"></span><br><span class="line">dbfilename dump6380.rdb</span><br><span class="line"></span><br><span class="line">replicaof localhost 6379</span><br></pre></td></tr></table></figure>

<p><strong><em>| 命令</em></strong></p>
<p>​        <code>INFO REPLICATION</code> 查看节点信息</p>
<p><strong><em>| 数据复制场景</em></strong></p>
<ol>
<li>主机宕机</li>
</ol>
<ol start="2">
<li>从机宕机</li>
</ol>
<ol start="3">
<li>主机宕机重启</li>
</ol>
<ol start="4">
<li>从机宕机重启</li>
</ol>
<p><strong><em>| 主从模式</em></strong></p>
<ol>
<li><p>主 - 从 - 从</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A[主机mater] --&gt;B(从机slave)</span><br><span class="line">A --&gt; C[从机slave]</span><br></pre></td></tr></table></figure>
</li>
<li><p>主 - 从（主） - 从</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A[主机mater] --&gt;B(从机slave)</span><br><span class="line">B --&gt; C[从机slave]</span><br></pre></td></tr></table></figure>
</li>
<li></li>
</ol>
<h4 id="四、Redis哨兵"><a href="#四、Redis哨兵" class="headerlink" title="四、Redis哨兵"></a>四、<strong><em>Redis</em></strong>哨兵</h4><p>​        主从复制中，主Redis节点可写，从节点可读。假设主节点宕机后，只能满足读可用，但是写不可用。哨兵模式正是为了解决这个场景。</p>
<p><strong><em>| 哨兵场景</em></strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A[主机mater] --&gt;B(从机slave)</span><br><span class="line">A --&gt; C[从机slave]	</span><br><span class="line">D[哨兵] --&gt; A</span><br><span class="line">D[哨兵] --&gt; B</span><br><span class="line">D[哨兵] --&gt; C</span><br></pre></td></tr></table></figure>

<p>​    哨兵模式的存在就是为了使Redis有更高的可用性，从Salve中选一个来做Master。哨兵可以是多台，甚至是哨兵集群。多为是心跳检测。</p>
<p><strong><em>| 哨兵选举</em></strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A[主机mater] --&gt;B(从机slave)</span><br><span class="line">A --&gt; C[从机slave]	</span><br><span class="line">D[哨兵] --&gt; A</span><br><span class="line">D --&gt; B</span><br><span class="line">D --&gt; C</span><br><span class="line">E[哨兵] --&gt; A</span><br><span class="line">E[哨兵] --&gt; B</span><br><span class="line">E[哨兵] --&gt; C</span><br><span class="line">E[哨兵] --&gt; D</span><br><span class="line">F[哨兵N个]</span><br></pre></td></tr></table></figure>

<p>如图，假设N个哨兵，链接Master，哨兵1检测到Master已经宕机，这个叫做主官下线，当M个哨兵（M是可以配置的）检测到主机Master下线后，哨兵回进行投票，结果由一个哨兵发起，将执行<code>故障转移</code>操作，通过发布订阅，切换各个哨兵，这叫客观下线。</p>
<p><strong><em>| 哨兵投票算法</em></strong></p>
<p>待补充</p>
<p><strong><em>| 哨兵配置修改</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">filename sentinel.conf</span><br><span class="line"></span><br><span class="line"><span class="comment">### 基础配置</span></span><br><span class="line">port 26379</span><br><span class="line"></span><br><span class="line"><span class="comment">### 监控信息</span></span><br><span class="line"><span class="comment">##sentinel monitor monstername host（不能用localhost） 端口 投票数</span></span><br><span class="line">sentinel monitor myredis 127.0.0.1 6379 1</span><br><span class="line"></span><br><span class="line"><span class="comment">##检测时间可以设置</span></span><br><span class="line">sentinel down-after-milliseconds &lt;master-name&gt; &lt;milliseconds&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">## failover 时间可以设置</span></span><br><span class="line">sentinel failover-timeout &lt;master-name&gt; &lt;milliseconds&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">## master down掉后执行通知脚本</span></span><br><span class="line">sentinel notification-script &lt;master-name&gt; &lt;script-path&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">## master 后执行重新config脚本</span></span><br><span class="line">sentinel client-reconfig-script mymaster /var/redis/reconfig.sh</span><br></pre></td></tr></table></figure>

<p><strong><em>| 启动哨兵</em></strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-sentinel 配置文件</span><br></pre></td></tr></table></figure>

<p>主机宕机</p>
<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E8%BF%9B%E9%98%B6%5Cimage-20210419185629124.png" alt="image-20210419185629124"></p>
<h4 id="五、Redis缓存穿透、击穿"><a href="#五、Redis缓存穿透、击穿" class="headerlink" title="五、Redis缓存穿透、击穿"></a>五、<strong><em>Redis</em></strong>缓存穿透、击穿</h4><p><strong><em>| 缓存穿透</em></strong>（查不到，全部查Mysql）</p>
<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E8%BF%9B%E9%98%B6%5Cimage-20210419191606408.png" alt="image-20210419191606408"></p>
<ul>
<li>解决方案：<ol>
<li>布隆过滤器（后面补充）</li>
<li>设置空缓存并且设置一些过期时间（缺点是牺牲了空间换取性能和高可用）</li>
</ol>
</li>
</ul>
<p><strong><em>| 缓存击穿</em></strong>（相同key量太大）</p>
<p><img src="D:%5Chill%5Cmyhexo%5Csource%5Cimages%5CRedis-%E8%BF%9B%E9%98%B6%5Cimage-20210419192312455.png" alt="image-20210419192312455"></p>
<ul>
<li>解决方案：<ol>
<li>热点数据过期时间设置</li>
<li>分布式锁，限制读写线程数量</li>
</ol>
</li>
</ul>
<h4 id="六、Redis雪崩"><a href="#六、Redis雪崩" class="headerlink" title="六、Redis雪崩"></a>六、<strong><em>Redis</em></strong>雪崩</h4><p>​    缓存雪崩，某一个时间段内，缓存集体失效， 或者Redis宕机！最致命的是Redis宕机或者断网。为了高可用，一版Linux会停掉某些服务，保证主要可用。</p>
<p><strong><em>|redis高可用解决方案</em></strong></p>
<ol>
<li><p>异地多活</p>
<p>多设置几台Redis，集群</p>
</li>
<li><p>限流降级</p>
<p>加锁、或者队列来控制数据写缓存的额线程数量</p>
</li>
<li><p>数据预热</p>
<p>先把可能的数据，先访问一遍，这样大量访问的数据就会在缓存中</p>
</li>
</ol>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/04/01/Nosql/Redis-%E5%9F%BA%E7%A1%80/" rel="next" title="Nosql/Redis-基础">
                <i class="fa fa-chevron-left"></i> Nosql/Redis-基础
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/05/07/%E5%B7%A5%E5%85%B7%E5%BA%94%E7%94%A8/MakeFile/" rel="prev" title="Makefile学习">
                Makefile学习 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">heyaojia</p>
              <p class="site-description motion-element" itemprop="description">游戏行业的一枚小鲜肉</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="git@gitee.com:hillcool/hillcool.git" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-gitee"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-进阶"><span class="nav-number">1.</span> <span class="nav-text">Redis - 进阶</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#一、Redis-配置详解"><span class="nav-number">1.1.</span> <span class="nav-text">一、Redis 配置详解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#二、Redis持久化"><span class="nav-number">1.2.</span> <span class="nav-text">二、Redis持久化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三、Redis-主从复制"><span class="nav-number">1.3.</span> <span class="nav-text">三、Redis 主从复制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#四、Redis哨兵"><span class="nav-number">1.4.</span> <span class="nav-text">四、Redis哨兵</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#五、Redis缓存穿透、击穿"><span class="nav-number">1.5.</span> <span class="nav-text">五、Redis缓存穿透、击穿</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#六、Redis雪崩"><span class="nav-number">1.6.</span> <span class="nav-text">六、Redis雪崩</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heyaojia</span>

  

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.0.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.0.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.0"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
